using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectSurvivor
{
    public class Global : Architecture<Global>
    {
        // 经验值
        public static BindableProperty<int> Exp = new BindableProperty<int>(0);
        
        // 等级
        public static BindableProperty<int> Level = new BindableProperty<int>(1);
        
        // 主角血量
        public static BindableProperty<int> HP = new BindableProperty<int>(3);
        
        // 主角最大血量
        public static BindableProperty<int> MaxHP = new BindableProperty<int>(3);
        
        // 剑解锁
        public static BindableProperty<bool> SimpleSwordUnlock = new BindableProperty<bool>(false);
        
        // 剑的攻击力
        public static BindableProperty<float> SimpleSwordDamage = new BindableProperty<float>(Config.InitSimpleSwordDamage);
        
        // 剑的攻击间隔
        public static BindableProperty<float> SimpleSwordDuration = new BindableProperty<float>(Config.InitSimpleSwordDuration);
        
        // 剑数量
        public static BindableProperty<int> SimpleSwordCount = new BindableProperty<int>(Config.InitSimpleSwordCount);
        
        // 剑的攻击范围
        public static BindableProperty<float> SimpleSwordRange = new BindableProperty<float>(Config.InitSimpleSwordRange);
        
        // 刀解锁
        public static BindableProperty<bool> SimpleKnifeUnlock = new BindableProperty<bool>(false);
        
        // 刀的攻击力
        public static BindableProperty<float> SimpleKnifeDamage = new BindableProperty<float>(Config.InitSimpleKnifeDamage);
        
        // 刀的攻击间隔
        public static BindableProperty<float> SimpleKnifeDuration = new BindableProperty<float>(Config.InitSimpleKnifeDuration);
        
        // 刀数量
        public static BindableProperty<int> SimpleKnifeCount = new BindableProperty<int>(Config.InitSimpleKnifeCount);
        
        // 刀可穿透数量
        public static BindableProperty<int> SimpleKnifeAttackCount = new BindableProperty<int>(Config.InitSimpleKnifeAttackCount);
        
        // 旋转剑解锁
        public static BindableProperty<bool> SimpleRotateSwordUnlock = new BindableProperty<bool>(false);
        
        // 旋转剑的攻击力
        public static BindableProperty<float> SimpleRotateSwordDamage = new BindableProperty<float>(Config.InitSimpleRotateSwordDamage);
        
        // 旋转剑的攻击速度
        public static BindableProperty<float> SimpleRotateSwordSpeed = new BindableProperty<float>(Config.InitSimpleRotateSwordSpeed);
        
        // 旋转剑数量
        public static BindableProperty<int> SimpleRotateSwordCount = new BindableProperty<int>(Config.InitSimpleRotateSwordCount);
        
        // 旋转剑的攻击范围
        public static BindableProperty<float> SimpleRotateSwordRange = new BindableProperty<float>(Config.InitSimpleRotateSwordRange);
        
        // 篮球解锁
        public static BindableProperty<bool> SimpleBallUnlock = new BindableProperty<bool>(false);
        
        // 篮球的攻击力
        public static BindableProperty<float> SimpleBallDamage = new BindableProperty<float>(Config.InitSimpleBallDamage);
        
        // 篮球的攻击速度
        public static BindableProperty<float> SimpleBallSpeed = new BindableProperty<float>(Config.InitSimpleBallSpeed);
        
        // 篮球数量
        public static BindableProperty<int> SimpleBallCount = new BindableProperty<int>(Config.InitSimpleBallCount);
        
        // 炸弹是否解锁
        public static BindableProperty<bool> BombUnlock = new BindableProperty<bool>(false);
        
        // 炸弹伤害
        public static BindableProperty<float> BombDamage = new BindableProperty<float>(Config.InitBombDamage);
        
        // 炸弹掉落概率
        public static BindableProperty<float> BombPercent = new BindableProperty<float>(Config.InitBombPercent);
        
        // 暴击率
        public static BindableProperty<float> CriticalRate = new BindableProperty<float>(Config.InitCriticalRate);
        
        // 额外伤害
        public static BindableProperty<float> DamageRate = new BindableProperty<float>(1f);
        
        // 额外飞行物数量
        public static BindableProperty<int> AdditionFlyingCount = new BindableProperty<int>(0);
        
        // 额外移动速度
        public static BindableProperty<float> MovementSpeedRate = new BindableProperty<float>(1f);
        
        // 拾取范围
        public static BindableProperty<float> CollectableArea = new BindableProperty<float>(Config.InitCollectableArea);
        
        // 超级武器
        public static BindableProperty<bool> SuperSimpleBall = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperSimpleKnife = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperSimpleRotateSword = new BindableProperty<bool>(false);
        public static BindableProperty<bool> SuperSimpleSword = new BindableProperty<bool>(false);
        
        // 计时
        public static BindableProperty<float> CurrentSeconds = new BindableProperty<float>(0f);
        
        // 金币
        public static BindableProperty<int> Coin = new BindableProperty<int>(0);
        
        // 经验掉落概率
        public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.3f);
        
        // 金币掉落概率
        public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.05f);
        
        // 重置数据
        public static void ResetData()
        {
            Exp.Value = 0;
            Level.Value = 1;
            HP.Value = MaxHP.Value;
            
            // 剑
            SimpleSwordUnlock.Value = false;
            SimpleSwordDamage.Value = Config.InitSimpleSwordDamage;
            SimpleSwordDuration.Value = Config.InitSimpleSwordDuration;
            SimpleSwordCount.Value = Config.InitSimpleSwordCount;
            SimpleSwordRange.Value = Config.InitSimpleSwordRange;
            
            // 刀
            SimpleKnifeUnlock.Value = false;
            SimpleKnifeDamage.Value = Config.InitSimpleKnifeDamage;
            SimpleKnifeDuration.Value = Config.InitSimpleKnifeDuration;
            SimpleKnifeCount.Value = Config.InitSimpleKnifeCount;
            SimpleKnifeAttackCount.Value = Config.InitSimpleKnifeAttackCount;
            
            // 旋转剑
            SimpleRotateSwordUnlock.Value = false;
            SimpleRotateSwordDamage.Value = Config.InitSimpleRotateSwordDamage;
            SimpleRotateSwordSpeed.Value = Config.InitSimpleRotateSwordSpeed;
            SimpleRotateSwordCount.Value = Config.InitSimpleRotateSwordCount;
            SimpleRotateSwordRange.Value = Config.InitSimpleRotateSwordRange;
            
            // 篮球
            SimpleBallUnlock.Value = false;
            SimpleBallDamage.Value = Config.InitSimpleBallDamage;
            SimpleBallSpeed.Value = Config.InitSimpleBallSpeed;
            SimpleBallCount.Value = Config.InitSimpleBallCount;
            
            // 炸弹
            BombUnlock.Value = false;
            BombDamage.Value = Config.InitBombDamage;
            BombPercent.Value = Config.InitBombPercent;
            
            // 暴击率
            CriticalRate.Value = Config.InitCriticalRate;
            
            // 额外伤害
            DamageRate.Value = 1f;
            
            // 额外飞行物
            AdditionFlyingCount.Value = 0;
            
            // 额外移动速度
            MovementSpeedRate.Value = 1f;
            
            // 超级武器
            SuperSimpleBall.Value = false;
            SuperSimpleKnife.Value = false;
            SuperSimpleRotateSword.Value = false;
            SuperSimpleSword.Value = false;
            
            // 拾取范围
            CollectableArea.Value = Config.InitCollectableArea;
            
            CurrentSeconds.Value = 0f;
            Interface.GetSystem<ExpUpgradeSystem>().ResetData();
        }
        
        [RuntimeInitializeOnLoadMethod]
        public static void AutoInit()
        {
            AudioKit.PlaySoundMode = AudioKit.PlaySoundModes.IgnoreSameSoundInGlobalFrames;
            ResKit.Init();
            
            UIKit.Root.SetResolution(1920,1080,1);
            
            var saveSystem = Interface.GetSystem<SaveSystem>();
            
            // 初始化最大血量
            MaxHP.Value = saveSystem.LoadInt(nameof(MaxHP), 3);
            
            MaxHP.Register(maxHP =>
            {
                saveSystem.SaveInt(nameof(MaxHP), maxHP);
            });
            HP.Value = MaxHP.Value;
            
            // 初始化金币
            Coin.Value = saveSystem.LoadInt(nameof(Coin), 0);
			
            // 永久存储金币
            Coin.Register(coin =>
            {
                saveSystem.SaveInt(nameof(Coin), coin);
            });
            
            // 金币掉落概率
            CoinPercent.Value = saveSystem.LoadFloat(nameof(CoinPercent), 0.05f);
            CoinPercent.Register(coinPercent =>
            {
                saveSystem.SaveFloat(nameof(CoinPercent), coinPercent);
            });
            
            // 经验掉落概率
            ExpPercent.Value = saveSystem.LoadFloat(nameof(ExpPercent), 0.3f);
            ExpPercent.Register(expPercent =>
            {
                saveSystem.SaveFloat(nameof(ExpPercent), expPercent);
            });
        }
        
        // 到下一个等级所需的经验值
        public static int ExpToNextLevel()
        {
            return 5 * Level.Value;
        }
        
        // 掉落物品
        public static void GeneratePowerUp(GameObject obj, bool IsTreasureChestEnemy)
        {
            if (IsTreasureChestEnemy)
            {
                PowerUpManager.Default.TreasureChest.Instantiate()
                    .Position(obj.Position())
                    .Show();
                return;
            }
            
            float random = Random.Range(0, 1f);
            if (random < ExpPercent.Value)
            {
                PowerUpManager.Default.Exp.Instantiate()
                    .Position(obj.Position())
                    .Show();
                return;
            }
            
            random = Random.Range(0, 1f);
            if (random < CoinPercent.Value)
            {
                PowerUpManager.Default.Coin.Instantiate()
                    .Position(obj.Position())
                    .Show();
                return;
            }
            
            random = Random.Range(0, 1f);
            if (random < 0.1f)
            {
                PowerUpManager.Default.HP.Instantiate()
                    .Position(obj.Position())
                    .Show();
                return;
            }

            if (BombUnlock.Value)
            {
                random = Random.Range(0, 1f);
                if (random < BombPercent.Value)
                {
                    PowerUpManager.Default.Bomb.Instantiate()
                        .Position(obj.Position())
                        .Show();
                    return;
                }
            }
            
            random = Random.Range(0, 1f);
            if (random < 0.1f)
            {
                PowerUpManager.Default.GetAllExp.Instantiate()
                    .Position(obj.Position())
                    .Show();
                return;
            }
        }

        protected override void Init()
        {
            RegisterSystem(new SaveSystem());
            RegisterSystem(new AchievementSystem());
            RegisterSystem(new CoinUpgradeSystem());
            RegisterSystem(new ExpUpgradeSystem());
        }
    }
}

